package com.example.app.page;

import android.app.Application;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
import com.example.app.R;
import com.example.app.config.RetrofitClient;
import com.example.app.manager.TokenManager;
import com.example.app.event.ParkingMapUpdateEvent;
import com.example.app.view.ParkingMapView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class HomeFragment extends Fragment {
    private EditText etNavigationSlotNumber;
    private Button btnNavigate;
    private ParkingMapView parkingMapView;

    private TokenManager tokenManager;
    private RetrofitClient retrofitClient;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home, container, false);

        retrofitClient = RetrofitClient.getInstance((Application) requireContext().getApplicationContext());
        tokenManager = retrofitClient.getTokenManager();

        etNavigationSlotNumber = view.findViewById(R.id.et_navigation_slot_number);
        btnNavigate = view.findViewById(R.id.btn_navigate);
        parkingMapView = view.findViewById(R.id.map_preview);

        btnNavigate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String slotNumberStr = etNavigationSlotNumber.getText().toString();
                if (!TextUtils.isEmpty(slotNumberStr)) {
                    try {
                        int slotNumber = Integer.parseInt(slotNumberStr);
                        if (validateSlotNumber(slotNumber)) {
                            parkingMapView.setNavigationTarget(slotNumber);
                        } else {
                            showToast("无效的车位编号");
                            etNavigationSlotNumber.setText("");
                        }
                    } catch (NumberFormatException e) {
                        showToast("请输入有效的数字");
                    }
                } else {
                    showToast("请输入导航目标车位编号");
                }
            }
        });
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onParkingMapUpdate(ParkingMapUpdateEvent event) {
        if (parkingMapView != null) {
            parkingMapView.getParkStatus();
        }
    }
    private boolean validateSlotNumber(int slotNumber) {
        return slotNumber >= 1 && slotNumber <= parkingMapView.getTotalSlots();
    }

    private boolean isSlotOccupied(int slotNumber) {
        String slotIndexStr = String.valueOf(slotNumber);
        return parkingMapView.getPlateParkIds().contains(slotIndexStr);
    }


    private void showToast(String message) {
        Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show();
    }
}